﻿using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Processing;


namespace Ls.ImageSharep.App
{
    public partial class UCImage : UserControl
    {
        public UCImage()
        {
            InitializeComponent();
        }
        private void btnchose_Click(object sender, EventArgs e)
        {
            if (!frmImagMain.Instance.PnlContainer.Controls.ContainsKey("UCTemplate"))
            {
                UCTemplate un = new UCTemplate();
                un.templateCallBack = DrawText;
                un.Dock = DockStyle.Fill;
                frmImagMain.Instance.PnlContainer.Controls.Add(un);
            }
            frmImagMain.Instance.PnlContainer.Controls["UCTemplate"].BringToFront();
            frmImagMain.Instance.BackButton.Visible = true;
        }
        private void DrawText()
        {
            //MessageBox.Show(LsApp.ListItems.Count().ToString());
            LsApp.WriteLog("templatedata", LsApp.ObjectToString(LsApp.ListItems));
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(frmImagMain.Instance.TemplateStream))
            {
                foreach (var item in LsApp.ListItems)
                {
                    image.Mutate(x => x.DrawText(LsApp.GetTextOptions(item.FontSize, item.WrappingLength, item.X, item.Y), item.Content, SixLabors.ImageSharp.Color.HotPink));
                }
                pbtemplate.Image = System.Drawing.Image.FromStream(LsApp.ImgToMemoryStream(image));
            }
        }

        private void btnpick_Click(object sender, EventArgs e)
        {
            //定义一个文件打开控件
            OpenFileDialog ofd = new OpenFileDialog();

            //设置打开对话框的标题
            ofd.Title = "请选择要打开的文件";
            //设置打开对话框可以多选
            ofd.Multiselect = false;
            //设置对话框打开的文件类型
            ofd.Filter = "图片文件|*.jpeg|图片文件|*.png|图片文件|*.jpg";
            //设置文件对话框当前选定的筛选器的索引
            ofd.FilterIndex = 2;
            //设置对话框是否记忆之前打开的目录
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择的文件完整路径
                string filePath = ofd.FileName;
                //获取对话框中所选文件的文件名和扩展名，文件名不包括路径
                string fileName = ofd.SafeFileName;

                using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    //定义二进制数组
                    byte[] buffer = new byte[1024 * 1024 * 5];
                    //从流中读取字节
                    fsRead.Read(buffer, 0, buffer.Length);
                    frmImagMain.Instance.TemplateStream = buffer;
                    pbtemplate.Image  =Image.FromStream(fsRead);

                    frmImagMain.Instance.PnlContainer.Controls.RemoveByKey("UCTemplate");
                    LsApp.ListItems.Clear();
                }
            }

        }

        private void UCImage_Load(object sender, EventArgs e)
        {
            pbtemplate.Image = System.Drawing.Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/invoice.png");
            this.pbtemplate.SizeMode = PictureBoxSizeMode.AutoSize;
            frmImagMain.Instance.TemplateStream = LsApp.ImgToByt(pbtemplate.Image);
        }
    }
}
